package com.wpre.springaop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-11-24
 * Time: 17:49
 */
@Slf4j
@Aspect
@Component
//统计方法执行的时间
public class TimeRecordAspect {
    //    @Around("execution(* com.wpre.springaop.controller.*.*(..))")
//    @Around("com.wpre.springaop.aspect.AspectDemo.pt()")
//    @Around("@annotation(com.wpre.springaop.aspect.TimeRecord)")
    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object timeRecord(ProceedingJoinPoint pjt) throws Throwable {
        //记录开始时间
        long start = System.currentTimeMillis();

        //执行目标方法
        Object ret = pjt.proceed();

        //记录结束时间
        long end = System.currentTimeMillis();

        log.info("{}耗时:{}ms", pjt.getSignature(), end - start);

        return ret;
    }
}
